package com.hamster.tools.i18nManger.model
{
	import com.hamster.tools.i18nManger.events.AppEvent;
	import com.hamster.tools.i18nManger.vo.PropertyFileVO;
	import com.hamster.tools.i18nManger.vo.PropertyItemVO;
	
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	
	import mx.collections.ArrayCollection;
	
	import org.puremvc.as3.patterns.proxy.Proxy;
	
	public class PropertyItemVOProxy extends Proxy
	{
		public static var NAME:String = "PropertyItemVOProxy";
		
		[Bindable] public var addedItemList:ArrayCollection = new ArrayCollection();
		
		public function PropertyItemVOProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		public function writeNewItemsToFile():void
		{
			for each (var item:PropertyItemVO in addedItemList) {
				var parentFile:PropertyFileVO = item.parentFile;
				var f:File = new File(parentFile.filePath);
				var stream:FileStream = new FileStream();
				if (!f.exists) {
					stream.open(f, FileMode.WRITE);
				} else {
					stream.open(f, FileMode.APPEND);
				}
				stream.writeUTFBytes('\n#autogen\n');
				stream.writeUTFBytes(item.key + "=" + escape(item.value).replace(/%u/g, '\\u'));
				stream.close();
			}
			addedItemList.removeAll();
			this.sendNotification(AppEvent.SAVE_NEW_ITEMS_RESPONSE);
		}
		
		public function translate():void
		{
//			var request:String = "http://api.microsofttranslator.com/V2/Ajax.svc/Translate" +
//				"?appId=Bearer " + encodeURIComponent("") +
//				"&from=" + encodeURIComponent(from) +
//				"&to=" + encodeURIComponent(to) +
//				"&text=" + encodeURIComponent(text)
		}
	}
}